package com.hyjoy.web.servlet;

import com.hyjoy.entity.Category;
import com.hyjoy.service.CategoryService;
import com.hyjoy.service.impl.CategoryServiceImpl;
import com.hyjoy.util.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by hyjoy on 2018/4/6.
 */
public class AdminCategoryServlet extends BaseServlet {
    /**
     * 添加分类
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.封装category对象
            Category c = new Category();
            c.setCid(UUIDUtils.getUUID());
            c.setCname(request.getParameter("cname"));

            //2.调用service完成添加操作
            CategoryService cs = new CategoryServiceImpl();
            cs.save(c);

            //3.重定向
            response.sendRedirect(request.getContextPath() + "/adminCategory?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return null;
    }

    /**
     * 跳转到添加页面
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String addUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/admin/category/add.jsp";
    }

    /**
     * 展示所有分类
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.调用service 获取所有的分类
            CategoryService cs = new CategoryServiceImpl();
            List<Category> list = cs.findList();

            //2.将返回值放入request域中 请求转发
            request.setAttribute("list", list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

        return "/admin/category/list.jsp";
    }
}
